# -*- coding:utf-8 -*-
"""
使用信号量解决创建用户时，密码明文保存的问题
"""
__author__ = "shu2015626"


from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver

from user_operation.models import UserFav


@receiver(post_save, sender=UserFav)
def add_userfav(sender, instance=None, created=False, **kwargs):
    """
    通过信号量，实现用户收藏商品后goods对象加1
    """
    if created:
        goods = instance.goods
        goods.fav_num += 1
        goods.save()


@receiver(post_delete, sender=UserFav)
def delete_userfav(sender, instance=None, created=False, **kwargs):
    """
    通过信号量，实现用户取消收藏商品后goods对象减1
    """
    goods = instance.goods
    goods.fav_num -= 1
    goods.save()

